<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>返回顶部</title>
    <style>
        .box {
            width: 500px;
            height: 600px;
            background-color: pink;
            margin: 200px auto;
        }
        .goback {
            display: none;
            position: fixed;
            right: 0;
            top: 300px;
            width: 100px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: skyblue;
            color: white;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <div class="goback">返回顶部</div>
</body>
<script>
    window.addEventListener('load',function(){
        var box = document.querySelector('.box')
        var goback = document.querySelector('.goback')
        console.log(box.offsetTop);
        window.addEventListener('scroll',function(){
            if(document.documentElement.scrollTop>=box.offsetTop){
                goback.style.display = 'block'
            }else{
                goback.style.display = 'none'
            }
        })
        goback.onclick=function(){
            document.documentElement.scrollTop=0
        }
    })
</script>
</html>